/**
 * Created by chong on 2017/4/26.
 */
function oper_point(objid,pkid,uid,point,type) {
    $("#dialog-form").dialog("open");
    SetRechargeDilog(pkid, uid, point,type);
}
//设置充值对话框
function SetRechargeDilog(pkid,uid,point,type)
{
	$("#hd_r_type").val("1");
	$("#hd_r_pkid").val(pkid);
	$("#la_r_uid").html(uid);
	//var point=$("#sp_point_"+pkid).html();//防止不是最新的
	$("#la_r_point").html(point);
	$("#tb_r_point").val("0");
	$("#hd_type").val(type);
	//$('#dialog-form').
	if (type==0)
	{
		$("#la_r_values").html("充入");
	}
	else {
        $("#la_r_values").html("扣除");
    }
}
//创建充值对话框
function CreateRechargeDailog() {
    //var thtml='<div class="btn-group" style="background-color: #1b9dec;"><input type="button" id="btn-btn" value="click me" onclick="toTestBox()" />' +
    var thtml = '<input type="hidden" id="hd_r_type"/><input type="hidden" id="hd_r_pkid" /><input type="hidden" id="hd_type"/><div id="dialog-form" title="代理额度操作">' +
        '<p style="font-size: 16px;">呢称:<label id="la_r_uid"></label> </p>' +
        '<p style="font-size: 16px;">余额:<label id="la_r_point">0</label> </p>' +
        '<p style="font-size: 16px;"><label id="la_r_values">充入</label>:<input type="text" id="tb_r_point" maxlength="10" onkeyup=\"value=value.replace(/[^\d]/g,"")\" /> </p>' +
        '</div>';
    $(".content-toolbar").append(thtml);
    //只能输入数字
    $('input#tb_r_point').keyup(function () {
        var c = $(this);
        if (/[^\d]/.test(c.val())) {//替换非数字字符
            var temp_amount = c.val().replace(/[^\d]/g, '');
            $(this).val(temp_amount);
        }
    });


    $("#dialog-form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "确定": function () {
                //alert('into');
                post_point();
            },
            "取消": function () {
                $("#dialog-form").dialog("close");
            }
        },
        close: function () {

        }
    });
}
function toTestBox()
{
	$( "#dialog-form" ).dialog( "open" );
	SetRechargeDilog(1,'andy',123);
	return;
	//alert('456');alert($('#btn-dialogBox'));
			$('#btn-dialogBox').dialogBox({
				type: 'correct',  //three type:'normal'(default),'correct','error',
						width: 300,
						height: 200,
				hasMask:true,
				hasClose: true,
				hasBtn: true,
				confirmValue: 'I am sure',
				confirm: function(){
					alert('this is callback function');
				},
				cancelValue: 'I will cancel',
				title: 'title text',
				content: 'dialog content text,image,html file'
			});

}
function post_point()
{
	var pkid=$("#hd_r_pkid").val();
	var uid=$("#la_r_uid").html();
	var tval=$("#tb_r_point").val();//alert(tval);
    var num = $("#hd_type").val();
    var nums = 0;
    var oper = '充入';
	//alert(isNaN(tval));
	if(isNaN(parseInt(tval)) || parseInt(tval)<=0) {
		alert('请输入数字');
		return;
	}
	else
	{
		if(num!=nums){
			oper = '扣除'
		}

		if (confirm("是否对" + uid + " 的额度 " +oper+ tval + " ？")) {
			//弹对话框
			//alert('wating,...');return;
            //alert(num);
			$.post('/xadmin/sysoper/agentpoint/', {"id": pkid, "point": tval,'type':num}, function (result) {
				//alert(result.ret);
				if (result.ret == 0) {
					//刷新并关闭对话框
					//$("#sp_point_"+pkid).html(result.point);
					$( "#dialog-form" ).dialog( "close" );
					//console.log($(id).attr('serverip'))
					window.location.reload();
				} else {
					alert(result.msg + "(" + result.ret + ")")
				}
			}, "json")
		}
	}
}

$(function() {
    //loadCss('/websys/static/css/jquery-ui.min.css');
    CreateRechargeDailog();
})



function oper_money(objid,pkid,uid,point,type) {
    $("#dialog-form_two").dialog("open");
    SetRechargeDilog_two(pkid, uid, point,type);
}
//设置充值对话框
function SetRechargeDilog_two(pkid,uid,point,type)
{
	$("#hd_r_type_two").val("1");
	$("#hd_r_pkid_two").val(pkid);
	$("#la_r_uid_two").html(uid);
	//var point=$("#sp_point_"+pkid).html();//防止不是最新的
	$("#la_r_point_two").html(point);
	$("#tb_r_point_two").val("0");
	$("#hd_type_two").val(type);
	//$('#dialog-form').
	if (type==0)
	{
		$("#la_r_values_two").html("充入");
	}
	else {
        $("#la_r_values_two").html("扣除");
    }
}
//创建充值对话框
function CreateRechargeDailog_two() {
    //var thtml='<div class="btn-group" style="background-color: #1b9dec;"><input type="button" id="btn-btn" value="click me" onclick="toTestBox()" />' +
    var thtml = '<input type="hidden" id="hd_r_type_two"/><input type="hidden" id="hd_r_pkid_two" /><input type="hidden" id="hd_type_two"/><div id="dialog-form_two" title="代理提成操作">' +
        '<p style="font-size: 16px;">呢称:<label id="la_r_uid_two"></label> </p>' +
        '<p style="font-size: 16px;">提成余额:<label id="la_r_point_two">0</label> </p>' +
        '<p style="font-size: 16px;"><label id="la_r_values_two">充入</label>:<input type="text" id="tb_r_point_two" maxlength="10" onkeyup=\"value=value.replace(/[^\d]/g,"")\" /> </p>' +
        '</div>';
    $(".content-toolbar").append(thtml);
    //只能输入数字
    $('input#tb_r_point_two').keyup(function () {
        var c = $(this);
        if (/[^\d]/.test(c.val())) {//替换非数字字符
            var temp_amount = c.val().replace(/[^\d]/g, '');
            $(this).val(temp_amount);
        }
    });


    $("#dialog-form_two").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,
        buttons: {
            "确定": function () {
                //alert('into');
                post_point_two();
            },
            "取消": function () {
                $("#dialog-form_two").dialog("close");
            }
        },
        close: function () {

        }
    });
}
function toTestBox_two()
{
	$( "#dialog-form_two" ).dialog( "open" );
	SetRechargeDilog_two(1,'andy',123);
	return;
	//alert('456');alert($('#btn-dialogBox'));
			$('#btn-dialogBox').dialogBox({
				type: 'correct',  //three type:'normal'(default),'correct','error',
						width: 300,
						height: 200,
				hasMask:true,
				hasClose: true,
				hasBtn: true,
				confirmValue: 'I am sure',
				confirm: function(){
					alert('this is callback function');
				},
				cancelValue: 'I will cancel',
				title: 'title text',
				content: 'dialog content text,image,html file'
			});

}
function post_point_two()
{
	var pkid=$("#hd_r_pkid_two").val();
	var uid=$("#la_r_uid_two").html();
	var tval=$("#tb_r_point_two").val();//alert(tval);
    var num = $("#hd_type_two").val();
    var nums = 0;
    var oper = '充入';
	//alert(isNaN(tval));
	if(isNaN(parseInt(tval)) || parseInt(tval)<=0) {
		alert('请输入数字');
		return;
	}
	else
	{
		if(num!=nums){
			oper = '扣除'
		}

		if (confirm("是否对" + uid + " 的提成 " +oper+ tval + " ？")) {
			//弹对话框
			//alert('wating,...');return;
            //alert(num);
			$.post('/xadmin/sysoper/agent_money/', {"id": pkid, "point": tval,'type':num}, function (result) {
				//alert(result.ret);
				if (result.ret == 0) {
					//刷新并关闭对话框
					//$("#sp_point_"+pkid).html(result.point);
					$( "#dialog-form_two" ).dialog( "close" );
					//console.log($(id).attr('serverip'))
					window.location.reload();
				} else {
					alert(result.msg + "(" + result.ret + ")")
				}
			}, "json")
		}
	}
}

$(function() {
    //loadCss('/websys/static/css/jquery-ui.min.css');
    CreateRechargeDailog_two();
})

function getQueryString(){
     result = {}
     r = new RegExp("[\?\&]([^\?\&]+)=([^\?\&]+)","g")
     a = r.exec(location.href)
     while(a)
	{    console.log(a)
		 result[a[1]]=a[2]
		  a = r.exec(location.href)
	}
     return result;

}
//状态操作
function oper_status(objid,pkid,val,uid)
{
    if(confirm("是否操作 "+ uid + "的状态")) {
        $.post('/xadmin/sysoper/agentstatus/', {"id":pkid, "type":val}, function(result){
            //alert(result.ret);
			if(result.ret == 0)
			{
					//console.log($(id).attr('serverip'))
					window.location.reload();
			}else
			{
				alert(result.msg + "("+result.ret +")")
			}
		 }, "json")

    }
}
function oper_ischeck(objid,pkid,val,uid)
{
    if(confirm("是否审核通过 "+ uid + "吗")) {
        $.post('/xadmin/sysoper/agentcheck/', {"id":pkid, "type":val}, function(result){
            //alert(result.ret);
			if(result.ret == 0)
			{
					//console.log($(id).attr('serverip'))
					window.location.reload();
			}else
			{
				alert(result.msg + "("+result.ret +")")
			}
		 }, "json")

    }
}
//修改
function oper_edit(objid,pkid,val,uid) {
	var cur_hef=window.location.href;
	var parms='';
	var ii_in=cur_hef.indexOf('?');
	if(ii_in>0) {
		parms = cur_hef.substring(ii_in, cur_hef.length)
	}
	window.location='/xadmin/websys/agentinfo/'+pkid+'/update/'+parms;
}
//过滤只显示自己的下线代理
function show_mynext(my_next)
{
	var hm_boxs=$(".dropdown-submenu");
				for(var ii=0;ii<hm_boxs.length;ii++)
				{//alert(hm_boxs[ii]);
					var cs = hm_boxs[ii].childNodes;
					var ok_ii=-1;
					 for(var jj=0;jj<cs.length;jj++)
					 {
					  if(cs[jj].nodeType==1 )
					  {
						  var cur_text=cs[jj].innerHTML;//alert(ii);alert(cur_text);
						  //alert(cs.length);alert(jj);
						  if(cur_text.indexOf('上级代理')>0)
						  {
							  ok_ii=ii;
						  }
						  else if(ok_ii==ii)
						  {
							  //ajax获取自己能看的下线
							  //var my_next=new Array("a","a1");
							  var del_arr=new Array();
							  //alert(cur_text);//alert(jj);
							  var cn_next = cs[jj].childNodes;
							  for(var kk=0;kk<cn_next.length;kk++)
							  {
								  if(cn_next[kk].nodeType==1 ) {
									  //alert(cn_next[kk].innerText);
									  var cur_txt=cn_next[kk].innerText;
									  var cur_index=cur_txt.indexOf('(');
									  if(cur_index>0)
									  {
										  var cur_uid=cur_txt.substring(0,cur_index);
										  var is_in=false;
											for(var nn=0;nn<my_next.length;nn++)
											{
												if(my_next[nn]==cur_uid){is_in=true;break;}
											}
										  if(!is_in){
											  del_arr.push(kk);
											  //alert(kk)//alert(cur_uid);
										  }
									  }
									  //是代理时空也不给
									  if(cur_txt=="空" ){del_arr.push(kk);}
								  }
							  }
							  //删除或隐藏
							  for(kk=0;kk<del_arr.length;kk++)
							  {//alert(del_arr[kk]);
								//cs[jj].removeChild(cn_next[del_arr[kk]]);
								  cn_next[del_arr[kk]].style.display="none";
							  }
						  }
							//alert(cs[i].id + " = " + cs[i].innerHTML)
					  }
					 }
					if(ok_ii>-1)
					{
						ok_ii=-1;
					}
				}
}
//判断是否有潜回按纽，帐号连接改为下级
function format_html()
{

	//alert(del_div);
	//del_div.style.display='none';
	//alert(123);
	var cur_hef=window.location.href;

	if(cur_hef.indexOf('/delete/')>0)
	{
		//alert('不能删除代理');
		//history.back();
	}

	$("table").find("tr").each(function() {
		//alert('ing...')
		var tdArr = $(this).children();
		//alert(tdArr.eq(1).children().length);//alert(123);
		//是否有选择框
		var is_check=0;
		var ch_arr=tdArr.eq(0).children();
		for(var ii=0;ii<ch_arr.length;ii++)
		{
			if(ch_arr[ii].type=="checkbox")
			{
				is_check=1;
				//tdArr.eq(0).html('');
				break;
			}
		}
		ch_arr = tdArr.eq(is_check).children();
		if (ch_arr.length >= 1) {//alert('oooo');
			var a_html = ch_arr[ch_arr.length - 1].innerHTML;
			//alert(a_html);
			var html_hef=ch_arr[ch_arr.length - 1].href;
			if(html_hef!=undefined) {
				///xadmin/websys/agentinfo/2/update/?_rel_parentid__id__exact=1
				var ii_in=cur_hef.indexOf('?');
				//alert(ii_in);alert(cur_hef)
				if(ii_in>0)//带参数
				{
					var tmp_arr=cur_hef.split('?');
					var s_start=tmp_arr[0];
					var s_end=tmp_arr[1];
					//alert(s_end);
					var tmp_list=html_hef.replace('/update/','').split('/');
					//alert(html_hef);
					if(html_hef.indexOf('?')>0)
					{
						var tt_arr=html_hef.split('?');
						tmp_list=tt_arr[0].replace('/update/','').split('/');
					}
					var cur_pid=tmp_list[tmp_list.length-1];//alert(cur_pid);

					if(s_end.indexOf('_rel_parentid__id__exact')>-1)//包含,替换掉
					{
						tmp_list=s_end.split('&');
						s_end='';
						for(var kk=0;kk<tmp_list.length;kk++)
						{
							if(tmp_list[kk].indexOf('_rel_parentid__id__exact')==-1)
							{
								if(s_end!=''){s_end+='&';}
								s_end+=tmp_list[kk]
							}
						}
						s_end+='&_rel_parentid__id__exact='+cur_pid;
						//alert(s_end);
					}
					else
					{
						s_end+="&_rel_parentid__id__exact="+cur_pid;
					}
					//alert(s_end);
					ch_arr[ch_arr.length - 1].href = '/xadmin/websys/agentinfo/?'+s_end;
				}
				else
				{
					var tmp_arr=html_hef.replace('/update/','').split('/');///xadmin/websys/agentinfo/123/update/
					ch_arr[ch_arr.length - 1].href = '/xadmin/websys/agentinfo/?_rel_parentid__id__exact='+tmp_arr[tmp_arr.length-1];
				}

			}
			//ch_arr[ch_arr.length-1].style.display='none';
			//tdArr.eq(1).append(a_html);
		}
	});


	//修改页面不用理
	if(cur_hef.indexOf('/update/')>0)
	{
		$("#id_mycid").val('123');
		return;
	}
	else
	{
		//隐藏删除连接
		if(cur_hef.indexOf('/add/')>0) {

		}
		else
		{
			//var del_div = $('.form-actions');
			//del_div.html('');
		}
	}
	var page_add=0;
	//隐藏新增页面的快加按纽
	if(cur_hef.indexOf('/add/')>0)
	{
		page_add=1;
		//alert($(".btn btn-default").html());
		//$(".btn btn-primary btn-sm btn-ajax pull-right").css('display', 'none');
		var a_box=document.getElementById("agentinfo_form").getElementsByTagName("a");
		//alert($("#agentinfo_form"));alert(a_box);
		for(var ii=0;ii<a_box.length;ii++)
		{
			if(a_box[ii].className=="btn btn-primary btn-sm btn-ajax pull-right")
			{
				// alert(a_box[ii].innerHTML);
				a_box[ii].style.display="none";
				break;
			}
		}
	}
	//最后一级不能开代理,过滤下拉
	var my_cid='';
	var my_next=new Array();
	//为毛post不行
	var tmp_obj=getQueryString();
	$.get('/xadmin/sysoper/agentclassid/', tmp_obj, function(result){
			//alert(result);
			my_cid=result.ret;
			my_next=result.next;
			//alert(my_cid);
			//是添加页面代理操作时
			if(result.add==0){$(".navbar-btn").css('display', 'none');}
			if(page_add==1)//默认选择第一个
			{
				//document.getElementById("id_parentid").selectedIndex=1
			}
			//隐藏当前登陆者的cid
			$("#id_mycid").val(my_cid);//alert($("#id_mycid"));

			//管理员不用理
			if(my_cid.length<3){return;}

			//show_mynext(my_next);

		 }, "json");

}
format_html();
function setSelect()
{
	var tt=$('#id_parentid_wrap_container .option');
	alert(tt.length);
	var div_sel=$('.selectize-dropdown-content .option');alert(div_sel.length);
	return;
	for(var ii=0;ii<div_sel.length;ii++)
	{
		alert(typeof (div_sel[ii]));alert(div_sel[ii]);
		//var div_next=div_sel[ii].childNodes;alert(div_next.length);
	}
	var  sel_par = document.getElementById('id_parentid');
				//sel_par[1].selected = true;//选中
	var sel_box=document.getElementsByName('parentid');
	for(var ii=0;ii<sel_box.length;ii++)
	{
		alert(sel_box[ii]);alert(sel_box[ii].options.length);
	}
				//alert(document.getElementById('id_parentid').options.length);
				for (var i = 0; i < sel_par.options.length; i++) {
					alert(sel_par.options[i].value);
					//alert(sel_par.options[i].selected);alert(sel_par.options[i].text);
				}
}
//修改时设置上级，帐号不可操作，密码设置为6个*
//$(function(){
//    //alert("第二种方法。");
//	var href=window.location.pathname;
//	//alert(href);
//	var t_ii=href.indexOf('/update/');
//	if(t_ii>0)
//	{
//		$("#id_account").attr("disabled", true);
//		//$("#id_parentid").each(function () {
//		//　　$("#" + this.id).attr("disabled", true);
//		//});
//		//$("#id_parentid_wrap_container").attr("readonly","true");
//		//$('#id_parentid_wrap_container').attr("disabled","desabled");
//		//$("#id_parentid").attr("disabled", true);
//		//$("#id_parentid_wrap_container").attr("disabled", true);
//		$("#id_password1").htmlText("******");
//		$("#id_password2").htmlText("******");
//	}
//});
function setContrlStatus() {
	var href=window.location.pathname;
	//alert(href);
	var t_ii=href.indexOf('/update/');
	if(t_ii>0) {
		$("#id_account").attr("disabled", true);
		$("#id_parentid").attr("disabled", true);
		$("#id_password1").htmlText("******");
		$("#id_password2").htmlText("******");
	}
}
//setContrlStatus();